﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace ControlLibrary.Extensions;

public class MonthOffsetConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return value is not DateTime date || parameter is not string str || !int.TryParse(str, out var offset)
            ? value
            : date.AddMonths(offset);
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return value is not DateTime date || parameter is not string str || !int.TryParse(str, out var offset)
            ? value
            : date.AddMonths(-offset);
    }
}